package com.example.demo.service.impl;

import com.example.demo.entity.Personnelmanagearea;
import com.example.demo.entity.Personnelmanageexceptionfile;
import com.example.demo.handle.AreaListParseHandler;
import com.example.demo.handle.ExceptionFileListParseHandler;
import com.example.demo.mapper.PersonnelmanageexceptionfileMapper;
import com.example.demo.service.PersonnelmanageexceptionfileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-23
 */
@Service
public class PersonnelmanageexceptionfileServiceImpl extends ServiceImpl<PersonnelmanageexceptionfileMapper, Personnelmanageexceptionfile> implements PersonnelmanageexceptionfileService {

    @Autowired
    private PersonnelmanageexceptionfileMapper personnelmanageexceptionfileMapper;

    @Override
    public Integer insertExceptionData(String exceptionFileXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            ExceptionFileListParseHandler exceptionFileListParseHandler = new ExceptionFileListParseHandler();
            try {
                // 读取xml文件
                parser.parse(exceptionFileXml, exceptionFileListParseHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + exceptionFileListParseHandler.getPersonnelmanageexceptionfileArrayList().size() + "个节点被存储");
            for (Personnelmanageexceptionfile personnelmanageexceptionfile : exceptionFileListParseHandler.getPersonnelmanageexceptionfileArrayList()) {
                System.out.println(personnelmanageexceptionfile.getCsMineCode());
                System.out.println(personnelmanageexceptionfile.getCsDataTime());
                System.out.println(personnelmanageexceptionfile.getPsPersonCard());
                System.out.println(personnelmanageexceptionfile.getPsMineEntertime());
                System.out.println(personnelmanageexceptionfile.getPsMineOuttime());
                System.out.println(personnelmanageexceptionfile.getPsAreaCode());
                System.out.println(personnelmanageexceptionfile.getPsAreaEntertime());
                System.out.println(personnelmanageexceptionfile.getPsStationCode());
                System.out.println(personnelmanageexceptionfile.getPsStationEntertime());
                System.out.println(personnelmanageexceptionfile.getPsShouldPlace());
                System.out.println(personnelmanageexceptionfile.getPsShouldTime());
                System.out.println(personnelmanageexceptionfile.getPsRealTime());
                System.out.println(personnelmanageexceptionfile.getPsSpepersonExp());
                System.out.println("----finish----");
                personnelmanageexceptionfileMapper.insertExceptionData(personnelmanageexceptionfile.getCsMineCode(),personnelmanageexceptionfile.getCsDataTime(),personnelmanageexceptionfile.getPsPersonCard(),personnelmanageexceptionfile.getPsMineEntertime(),personnelmanageexceptionfile.getPsMineOuttime(),personnelmanageexceptionfile.getPsAreaCode(),personnelmanageexceptionfile.getPsAreaEntertime(),personnelmanageexceptionfile.getPsStationCode(),personnelmanageexceptionfile.getPsStationEntertime(),personnelmanageexceptionfile.getPsShouldPlace(),personnelmanageexceptionfile.getPsShouldTime(),personnelmanageexceptionfile.getPsRealTime(),personnelmanageexceptionfile.getPsSpepersonExp());
            }
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
